﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Sunboxsoft.WinForm;
using WebRef = Sunboxsoft.ASMS.Client.Domain.WebRef;
using Service = Sunboxsoft.ASMS.Client.Domain.Service;
using Sunboxsoft.Common;
namespace Sunboxsoft.ASMS.Client.UI.Management
{
    public partial class CreatePayItemView : UserControl, IView
    {
        public CreatePayItemView()
        {
            InitializeComponent();
        }

        #region IView 成员

        public void RegisterAction(ActionNodeCollection collection)
        {
            collection.AddAction(new ActionNode("save", "保存", btnSave_Click))
                .AddAction(new ActionNode("cancel", "取消", btnCancel_Click));
        }
        public void Resume(ViewResumeArgs args)
        {

        }
        public void Active(ViewActiveArgs args)
        {
            BindType();
        }
        public ViewHeader GetHeader()
        {
            return new ViewHeader("");
        }
        #endregion

        #region Method
        /// <summary>
        /// 获取选中的类别
        /// </summary>
        /// <returns></returns>
        public int GetStatus()
        {
            Domain.IntCodeTextItem item = this.lupType.GetSelectedDataRow() as Domain.IntCodeTextItem;
            if (item == null)
            {
                return Domain.Constant.BookingBillStatus.All;
            }
            return item.Code;
        }

        /// <summary>
        /// 数据检查
        /// </summary>
        /// <returns></returns>
        private bool ValidateData()
        {
            if (string.IsNullOrEmpty(this.txtName.Text.Trim()))
            {
                UIContext.Current.ViewManager.ShowWarning("请输入名称！");
                return false;
            }
            if (GetStatus() == 0)
            {
                UIContext.Current.ViewManager.ShowWarning("请选择类型！");
                return false;
            }
            if (string.IsNullOrEmpty(this.txtAmount.Text.Trim()))
            {
                UIContext.Current.ViewManager.ShowWarning("请输入金额！");
                return false;
            }
            else if (Convert.ToDecimal(this.txtAmount.EditValue) < 0)
            {
                UIContext.Current.ViewManager.ShowWarning("输入金额小于0，请确认！");
                return false;
            }
            return true;
        }
        /// <summary>
        /// 添加付费项目
        /// </summary>
        private void CreatePayItem()
        {
            WebRef.EPayItem payItem = new WebRef.EPayItem();
            payItem.Name = this.txtName.Text.Trim();
            payItem.BusinessId = ModuleContext.Current.BusinessId;
            payItem.StationId = ModuleContext.Current.StationId;
            payItem.Type = GetStatus();
            payItem.Amount = Convert.ToDecimal(this.txtAmount.Text.Trim());
            payItem.Description = this.txtRemark.Text.Trim();

            this.ShowMask("loading...");
            UIContext.Current.InvokeAsync(() =>
            {
                ServiceResult result = new Service.CreateService().CreatePayItem(payItem);

                this.BeginInvoke(new Action<ServiceResult>((p) =>
                {
                    this.HideMask();
                    if (p.IsSuccess)
                    {
                        UIContext.Current.ViewManager.CloseView(true, null);
                    }
                    else
                    {
                        UIContext.Current.ViewManager.ShowError(p.Message);
                    }
                }), result);
            });
        }

        /// <summary>
        /// 绑定类型
        /// </summary>
        private void BindType()
        {
            this.lupType.Properties.DataSource = Domain.Constant.PayItemTypes.Enumerate();
            this.lupType.Properties.DisplayMember = "Text";
            this.lupType.ItemIndex = 0;
        }
        #endregion

        #region Action
        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSave_Click(object sender, object e)
        {
            try
            {
                if (ValidateData())
                {
                    CreatePayItem();
                }
            }
            catch (Exception ex)
            {
                UIContext.Current.ViewManager.ShowError(ex.Message);
            }
        }

        /// <summary>
        /// 取消
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCancel_Click(object sender, object e)
        {
            UIContext.Current.ViewManager.CloseView(false, null);
        }
        #endregion
    }
}
